/*
Copyright (C) 2012 Brady Vidovic

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package xmltvdb.tools;

import btv.logger.BTVLogLevel;
import btv.tools.BTVProps;
import java.util.LinkedHashMap;
import java.io.File;
import java.util.LinkedHashSet;
import static xmltvdb.tools.Constants.*;
import static btv.tools.BTVTools.*;

/**
 * Static param class. Loads configuration params from .properties file
 * @author bvidovic
 */
public class Config extends BTVProps{
    
    public String WATCH_FOLDER, OUTPUT_XML_FILE, FTR_WEBSERVICE_URL, LOG_LEVEL, EMAIL_FROM_ADDRESS;
    public int MAX_TVDB_SERIES_LOOKUP, HTTP_REQUEST_RETRIES, WAIT_SECOND_BETWEEN_RETIRES, HTTP_CONNECT_TIMEOUT, HTTP_READ_TIMEOUT, MAX_WAIT_HOURS_FOR_NEW_XMLTV;
    public BTVLogLevel EMAIL_IF;
    public boolean ONLY_LOOKUP_SCHEDULED_SHOWS, EMAIL_NOTIFICATIONS, CACHE_TO_DISK;
    public LinkedHashSet<String> EXCLUDED_CATEGORIES, MOVIE_CATEGORIES, ZAP2IT_SOURCE_INFO_NAMES, NOTIFICATION_RECIPIENTS;
    public LinkedHashMap<String,String> TVDB_TRANSLATIONS;
    public Config(File propertiesFile)
    {
        super(propertiesFile);        
        
        WATCH_FOLDER =getTrimmedString("WATCH_FOLDER");
        Logger.DEBUG("WATCH_FOLDER = " + WATCH_FOLDER);
        
        OUTPUT_XML_FILE = getTrimmedString("OUTPUT_XML_FILE");
        Logger.DEBUG("OUTPUT_XML_FILE = " + OUTPUT_XML_FILE);
        
        ONLY_LOOKUP_SCHEDULED_SHOWS = getBoolean("ONLY_LOOKUP_SCHEDULED_SHOWS",true);
        Logger.DEBUG("ONLY_LOOKUP_SCHEDULED_SHOWS = " + ONLY_LOOKUP_SCHEDULED_SHOWS);
        
        if(ONLY_LOOKUP_SCHEDULED_SHOWS)
        {
            FTR_WEBSERVICE_URL = trimTrailingSlashes(getTrimmedString("FTR_WEBSERVICE_URL"));
            Logger.DEBUG("FTR_WEBSERVICE_URL = " + FTR_WEBSERVICE_URL);
            if(!valid(FTR_WEBSERVICE_URL)) 
                Logger.ERROR("ONLY_LOOKUP_SCHEDULED_SHOWS=true, but no FTR_WEBSERVICE_URL specified!");                        
        }
        
        EXCLUDED_CATEGORIES = getPipeDelimitedLowercaseTrimmedSet("EXCLUDED_CATEGORIES");
        Logger.DEBUG("EXCLUDED_CATEGORIES = " + EXCLUDED_CATEGORIES);
              
        MOVIE_CATEGORIES = getPipeDelimitedLowercaseTrimmedSet("MOVIE_CATEGORIES");
        Logger.DEBUG("MOVIE_CATEGORIES = " + MOVIE_CATEGORIES);
        
        ZAP2IT_SOURCE_INFO_NAMES = getPipeDelimitedLowercaseTrimmedSet("ZAP2IT_SOURCE_INFO_NAMES");
        Logger.DEBUG("ZAP2IT_SOURCE_INFO_NAMES = " + ZAP2IT_SOURCE_INFO_NAMES);
        
        
        MAX_TVDB_SERIES_LOOKUP = getInt("MAX_TVDB_SERIES_LOOKUP",5);                
        Logger.DEBUG("MAX_TVDB_SERIES_LOOKUP = " + MAX_TVDB_SERIES_LOOKUP);
        
        TVDB_TRANSLATIONS = getMap("TVDB_TRANSLATION","|",true);//lowercase keys
        Logger.DEBUG("TVDB_TRANSLATIONS = "+ TVDB_TRANSLATIONS);
        
        HTTP_REQUEST_RETRIES = getInt(("HTTP_REQUEST_RETRIES"),3);                
        Logger.DEBUG("HTTP_REQUEST_RETRIES = " + HTTP_REQUEST_RETRIES);
        
        WAIT_SECOND_BETWEEN_RETIRES = getInt("WAIT_SECOND_BETWEEN_RETIRES",15);                
        Logger.DEBUG("WAIT_SECOND_BETWEEN_RETIRES = " + WAIT_SECOND_BETWEEN_RETIRES);
        
        HTTP_CONNECT_TIMEOUT = getInt("HTTP_CONNECT_TIMEOUT",20);                
        Logger.DEBUG("HTTP_CONNECT_TIMEOUT = " + HTTP_CONNECT_TIMEOUT);
        
        HTTP_READ_TIMEOUT = getInt("HTTP_READ_TIMEOUT",60);                
        Logger.DEBUG("HTTP_READ_TIMEOUT = " + HTTP_READ_TIMEOUT);
        
        CACHE_TO_DISK = getBoolean("CACHE_TO_DISK", false);
        Logger.DEBUG("CACHE_TO_DISK = "+ CACHE_TO_DISK+", CACHE_TO_MEMORY = "+ (!CACHE_TO_DISK));
        
        MAX_WAIT_HOURS_FOR_NEW_XMLTV = getInt("MAX_WAIT_HOURS_FOR_NEW_XMLTV", 60);
        Logger.DEBUG("MAX_WAIT_HOURS_FOR_NEW_XMLTV = "+ MAX_WAIT_HOURS_FOR_NEW_XMLTV);
        
        EMAIL_NOTIFICATIONS = getBoolean("EMAIL_NOTIFICATIONS", false);
        Logger.DEBUG("EMAIL_NOTIFICATIONS = "+ EMAIL_NOTIFICATIONS);        
        if(EMAIL_NOTIFICATIONS)
        {
            String strEMAIL_IF = getString("EMAIL_IF", BTVLogLevel.ERROR.toString()).trim().toUpperCase();
            if("WARNING".equals(strEMAIL_IF)) strEMAIL_IF="WARN";//helper conversion                
            try
            {
                EMAIL_IF = BTVLogLevel.valueOf(strEMAIL_IF);
            }
            catch(IllegalArgumentException unknownLevel)
            {
                EMAIL_IF = BTVLogLevel.ERROR;
                Logger.WARN("EMAIL_IF level \""+strEMAIL_IF+"\" is unknown. Will use default of " + EMAIL_IF);
            }
            Logger.DEBUG("EMAIL_IF = "+ EMAIL_IF);        
            NOTIFICATION_RECIPIENTS = getSet("NOTIFICATION_RECIPIENTS", "|", false, true);
            EMAIL_FROM_ADDRESS = getString("FROM_ADDRESS", "error_notification-FTR-XBMC");            
        }
    }            
    
}
